#!/bin/sh
#
# Run a simple backup then create some
#   new files, do an Incremental and restore those two files.
#
TestName="comment-test"
JobName=Incremental
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmpsrc}"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/flat-c.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

mkdir -p ${tmpsrc}
cp -p ${tmp}/data/*.c ${tmpsrc}
echo "${tmpsrc}/ficheriro1.txt" >${tmp}/restore-list
echo "${tmpsrc}/ficheriro2.txt" >>${tmp}/restore-list

change_jobname CompressedTest $JobName
start_test

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
run comment="This is a comment for $JobName" job=$JobName yes
status client
@sleep 5
status dir
label storage=File volume=TestVolume001
mount storage=File
wait
messages
@$out $tmp/log3.out
sql
SELECT Comment FROM Job WHERE JobId=1;

quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
#
# Now create two new files to be restored later
#
sleep 1
echo "ficheriro1.txt" >${tmpsrc}/ficheriro1.txt
echo "ficheriro2.txt" >${tmpsrc}/ficheriro2.txt

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log1.out
@# Force Incremental on the second Volume
update volume=TestVolume001 VolStatus=Used
run comment="This is the incremental job" level=Differential job=$JobName yes
@sleep 5
status dir
label storage=File volume=TestVolume002
mount storage=File
wait
messages
@$out $tmp/log3.out
sql
SELECT Comment FROM Job WHERE JobId=2;

quit
END_OF_DATA

run_bconsole

cat <<END_OF_DATA >${tmp}/bconcmds
@$out /dev/null
messages
@$out ${tmp}/log2.out
@#
@# now do a restore
@#
restore comment="Ticket #1220 requested by toto@susan.org" where=${tmp}/susan-restores storage=File file=<${tmp}/restore-list
yes
status dir
wait
messages
@$out $tmp/log3.out
sql
SELECT Comment FROM Job WHERE JobId=3;

quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_susan

check_two_logs
check_restore_files_diff `cat ${tmp}/restore-list`

grep "This is a comment for $JobName" $tmp/log3.out >/dev/null
if [ $? != 0 ]; then
    print_debug "ERR: Can't find comment"
    estat=1
fi
grep "Ticket #1220 requested by toto@susan.org" $tmp/log3.out >/dev/null
if [ $? != 0 ]; then
    print_debug "ERR: Can't find comment"
    estat=1
fi
grep "This is the incremental job" $tmp/log3.out >/dev/null
if [ $? != 0 ]; then
    print_debug "ERR: Can't find comment"
    estat=1
fi
end_test
